﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;


namespace WindowsFormsApplication3
{
    using alldataset;
    using alldataset.fullldatasetTableAdapters;

    public partial class NewEmpRegFormcs : Form
        
    {
        
        Transaction.EmployeeTransaction emptransaction = null;
        
        DataBean.EmployeeDatabean empdatabean = null;
        public NewEmpRegFormcs()
        {
            InitializeComponent();
            emptransaction = new Transaction.EmployeeTransaction();
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void button3_Click(object sender, EventArgs e)
        {
            this.Close();
            

        }
        /// <summary>
        /// validates thr controls
        /// </summary>
        /// <returns></returns>
        public Boolean validationcontrol()
        {
            Boolean sucess = false;
            if (txtname.Text.Trim() == null || txtname.Text.Trim() == "")
            {
                lblStatus.Text = "Enter Name";
                txtname.Focus();
                txtname.Text = "";
            }
            else if (txtDesignation.Text.Trim() == null || txtDesignation.Text.Trim() == "")
            {

                txtDesignation.Focus();
                lblStatus.Text = "Enter an Designation";
                txtDesignation.Text = "";
            }

            else if (txtpassportno.Text.Trim() == null || txtpassportno.Text.Trim() == "")
            {

                txtpassportno.Focus();
                lblStatus.Text = "Enter an valid Passport No#";
                txtpassportno.Text = "";
            }
            else if (mtxtphoneno.Text.Trim() == null || mtxtphoneno.Text.Trim() == "")
            {

                mtxtphoneno.Focus();
                lblStatus.Text = "Enter a Phone no";
                mtxtphoneno.Text = "";
            }
           
            else if (rhtaddress.Text.Trim() == null || rhtaddress.Text.Trim() == "")
            {

                rhtaddress.Focus();
                lblStatus.Text = "Enter an Adress";
                rhtaddress.Text = "";
            }
            else if (mtxtbasic.Text.Trim() == null || mtxtbasic.Text.Trim() == "")
            {

                mtxtbasic.Focus();
                lblStatus.Text = "Enter Basic Salary";
                mtxtbasic.Text = "";
            }
            else if (mtxtTa.Text.Trim() == null || mtxtTa.Text.Trim() == "")
            {

                mtxtTa.Focus();
                lblStatus.Text = "Enter an Travel Allowance";
                mtxtTa.Text = "";
            }
            else if (mtxtHA.Text.Trim() == null || mtxtHA.Text.Trim() == "")
            {

                mtxtHA.Focus();
                lblStatus.Text = "Enter Housing Allowance";
                mtxtHA.Text = "";
            }
            else if (mtxtothers.Text.Trim() == null || mtxtothers.Text.Trim() == "")
            {

                mtxtothers.Focus();
                lblStatus.Text = "Enter Other";
                mtxtothers.Text = "";
            }
            else if (txtvisa.Text.Trim() == null || txtvisa.Text.Trim() == "")
            {

                txtvisa.Focus();
                lblStatus.Text = "Enter Visa No";
                txtvisa.Text = "";
            }
            else if (txtuaeid.Text.Trim() == null || txtuaeid.Text.Trim() == "")
            {

                txtuaeid.Focus();
                lblStatus.Text = "Enter an uae id";
                txtuaeid.Text = "";
            }
            else if (txtpassportno.Text.Trim() == null || txtpassportno.Text.Trim() == "")
            {

                txtpassportno.Focus();
                lblStatus.Text = "Enter Passport No";
                txtpassportno.Text = "";
            }
            else if (mtxtTotal.Text.Trim() == null || mtxtTotal.Text.Trim() == "")
            {

                mtxtTotal.Focus();
                lblStatus.Text = "Enter salary Datas";
               
            }
            if (cmbemployeetype.Text == null || cmbemployeetype.Text.Trim()== "")
            {
                cmbemployeetype.Focus();
                lblStatus.Text = "Enter Employee Type";

            }
            if (cmbincurrency.Text == null || cmbincurrency.Text.Trim() == "")
            {
                cmbincurrency.Focus();
                lblStatus.Text = "Enter Payment Currency";

            }
            if (cmbemployeetype.Text == null || cmbemployeetype.Text.Trim() == "")
            {
                cmbemployeetype.Focus();
                lblStatus.Text = "Enter Employee Type";

            }
            else
            {

                sucess = true;
            }
            return sucess;
        }

        /// <summary>
        /// insert data to datatable
        /// </summary>
        public void insertdatatotable()
        {
            empdatabean = new DataBean.EmployeeDatabean();
            txtempcode.Text = "Emp" + txtvisa.Text;
           
            empdatabean.Employename= txtname.Text;
            if (rbtfemale.Checked == true)
            {
                empdatabean.Gender = "Female";
            }
            else
            {
                empdatabean.Gender = "Male";
            }
            empdatabean.Designation = txtDesignation.Text;
            empdatabean.Nationality1 = cmbnationality.Text;
            empdatabean.Visano = txtvisa.Text;
            empdatabean.Uaeid = txtuaeid.Text;
            empdatabean.Passportno = txtpassportno.Text;
            empdatabean.Phoneno = mtxtphoneno.Text;
            empdatabean.Adress = rhtaddress.Text;
            empdatabean.Basicsalary = int.Parse( mtxtbasic.Text);
            empdatabean.Ha = int.Parse(mtxtbasic.Text);
            empdatabean.Ta = int.Parse(mtxtbasic.Text);
            empdatabean.Others = int.Parse(mtxtbasic.Text);
            empdatabean.Total = int.Parse(mtxtTotal.Text);
            empdatabean.Joiningdate = dateTimePicker1.Value.Date;
            empdatabean.Employetype = cmbemployeetype.Text;
            empdatabean.Employeecode = txtempcode.Text;
            empdatabean.Incurrency = cmbincurrency.Text;

        }
        /// <summary>
        /// when datas are entered and submit button clicked in
        /// registration panel
        /// validation is done here
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)

        {
            EmployeedatatableTableAdapter empadapt = new EmployeedatatableTableAdapter();
            empadapt.Connection = new System.Data.OleDb.OleDbConnection(Program.ConnStr);
           
            if (validationcontrol())
            {
                insertdatatotable();
                try
                {
                    emptransaction.insertEmpData(empdatabean);
                    MessageBox.Show("Sucessfully Entered Employee Data");
                                      
                    DataTable dt= empadapt.Getemployeepk(empdatabean.Employename,empdatabean.Passportno);                 
                    
                    int empepk = int.Parse(dt.Rows[0][15].ToString());

                    try
                    {
                        emptransaction.insertempsalarydata(empepk, empdatabean);
                        MessageBox.Show("New Employee Detail Added " + empdatabean.Employename +"      Emp code" +empdatabean.Employeecode);

                      //  MessageBox.Show("Add Documents of " + empdatabean.Employeecode);
                       // addphoto(empepk);
                       // this.Close();
                        lblemppk.Text = empepk.ToString(); ;
                        chkbxEdit.Checked = true;
                        registerpanel.Enabled = false;
                      //  pnlprofilephoto.Enabled = true;
                      //  button1.Enabled = true;
                        
                        
                    }
                    catch (Exception expx)
                    {
                       MessageBox.Show("Exception at  emptransaction.insertempsalarydata(empepk, empdatabean);"+expx);
                       if (emptransaction.DeleteEmpData(empepk))
                       {

                           this.Close();
                       }
                       else
                       {
                           MessageBox.Show("The Employee details in the database is corrupted Please manually delete the values");
                       }
                    }
                
                
                }
                catch (Exception exp)
                {
                    
                   MessageBox.Show("Exception at  emptransaction.insertEmpData(empdatabean);"+exp);
                    this.Close();
                  
                }
               
            }
            else
            {

            }
        }
              
        /// <summary>
        /// button submit on document section clicked
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            if (button1.Text == "Add Photo")
            {
                using (OpenFileDialog opnfldg = new OpenFileDialog())
                {

                    opnfldg.Title = "Open Photo";
                    if (opnfldg.ShowDialog() == DialogResult.OK)
                    {
                        pctprofilephoto.Image = new Bitmap(opnfldg.FileName);
                        button1.Text = "Submit";
                    }
                }
            }
        }



        private void pictureBox4_Click(object sender, EventArgs e)
        {
            using (OpenFileDialog opnfldg = new OpenFileDialog())
            {
                   opnfldg.Title = "Open Photo";
                   opnfldg.Filter = "Image Files (JPEG,GIF,BMP,PNG,ICO)|*.jpg;*.jpeg;*.gif;*.bmp;*.png;*ico";

                    if (opnfldg.ShowDialog() == DialogResult.OK)
                    {
                        pctpassprt1.Image = Image.FromFile(opnfldg.FileName);
                       
                  
                 }
        }

        }

        private void pctpassprt2_Click(object sender, EventArgs e)
        {
            using (OpenFileDialog opnfldg = new OpenFileDialog())
            {
                   opnfldg.Title = "Open Photo";
                   opnfldg.Filter = "Image Files (JPEG,GIF,BMP,PNG,ICO)|*.jpg;*.jpeg;*.gif;*.bmp;*.png;*ico";

                    if (opnfldg.ShowDialog() == DialogResult.OK)
                    {
                        pctpassprt2.Image = Image.FromFile(opnfldg.FileName);
                       
                  
                 }
        }
        }

        private void pctvisa1_Click(object sender, EventArgs e)
        {
             using (OpenFileDialog opnfldg = new OpenFileDialog())
            {
                   opnfldg.Title = "Open Photo";
                   opnfldg.Filter = "Image Files (JPEG,GIF,BMP,PNG,ICO)|*.jpg;*.jpeg;*.gif;*.bmp;*.png;*ico";

                    if (opnfldg.ShowDialog() == DialogResult.OK)
                    {
                        pctvisa1.Image = Image.FromFile(opnfldg.FileName);
                       
                  
                 }
        }
        }

        private void pctdocument1_Click(object sender, EventArgs e)
        {
             using (OpenFileDialog opnfldg = new OpenFileDialog())
            {
                   opnfldg.Title = "Open Photo";
                   opnfldg.Filter = "Image Files (JPEG,GIF,BMP,PNG,ICO)|*.jpg;*.jpeg;*.gif;*.bmp;*.png;*ico";

                    if (opnfldg.ShowDialog() == DialogResult.OK)
                    {
                        pctdocument1.Image = Image.FromFile(opnfldg.FileName);
                       
                  
                 }
        }
        }

        private void NewEmpRegFormcs_Load(object sender, EventArgs e)
        {
            pctprofilephoto.Image = Image.FromFile("D:\\16-4-2012\\WindowsFormsApplication3\\projectimages\\docement.jpeg");
            pctpassprt2.Image = Image.FromFile("D:\\16-4-2012\\WindowsFormsApplication3\\projectimages\\docement.jpeg");
            pctdocument1.Image = Image.FromFile("D:\\16-4-2012\\WindowsFormsApplication3\\projectimages\\docement.jpeg");
            pctpassprt1.Image = Image.FromFile("D:\\16-4-2012\\WindowsFormsApplication3\\projectimages\\docement.jpeg");
            pctdocument1.Image = Image.FromFile("D:\\16-4-2012\\WindowsFormsApplication3\\projectimages\\docement.jpeg");

        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (chkbxEdit.Checked == true)
            {
                grpdocuments.Enabled = true;

            }
            if (chkbxEdit.Checked == false)
            {
                grpdocuments.Enabled = false;

            }
        }

        private void btnphotosubmit_Click(object sender, EventArgs e)
        {
            
            if (btnphotosubmit.Text == "Submit")
            {
                addphoto(int.Parse(lblemppk.Text));
            }

        }




        /// <summary>
        /// save the photo into system
        /// </summary>
        /// <param name="empepk"></param>


        public void  addphoto(int empepk)
        {
            ArrayList emplst = new ArrayList();

            if (validationcontrol())
            {                               
                
                string flepth1 = "D:\\16-4-2012\\WindowsFormsApplication3\\projectimages\\emp1" + empepk + "psprt1.Jpeg";
                string flepth2 = "D:\\16-4-2012\\WindowsFormsApplication3\\projectimages\\emp1" + empepk + "psprt2.Jpeg";
                string flepth3 = "D:\\16-4-2012\\WindowsFormsApplication3\\projectimages\\emp1" + empepk + "visa1.Jpeg";
                string flepth4 = "D:\\16-4-2012\\WindowsFormsApplication3\\projectimages\\emp1" + empepk + "docement1.Jpeg";
                string flepth5 = "D:\\16-4-2012\\WindowsFormsApplication3\\projectimages\\emp1" + empepk + "profileimage.Jpeg";
                pctpassprt1.Image.Save(@flepth1, System.Drawing.Imaging.ImageFormat.Jpeg);
                pctpassprt2.Image.Save(@flepth2, System.Drawing.Imaging.ImageFormat.Jpeg);
                pctvisa1.Image.Save(@flepth3, System.Drawing.Imaging.ImageFormat.Jpeg);
                pctdocument1.Image.Save(@flepth4, System.Drawing.Imaging.ImageFormat.Jpeg);
                pctprofilephoto.Image.Save(@flepth5, System.Drawing.Imaging.ImageFormat.Jpeg);
                emplst.Add(empepk);
                emplst.Add(flepth1);
                emplst.Add(flepth2);
                emplst.Add(flepth3);
                emplst.Add(flepth4);
                emplst.Add(flepth5);
                if (emptransaction.insertemployeephotos(emplst))
                {
                    MessageBox.Show("Documents Images Added");
                }

            }
         
        }

        /// <summary>
        /// calculates the total salary ehen any of salary textis changed
        /// </summary>
        public void totalsumaddaction()
        {
            

                    Int32 amount = int.Parse(mtxtbasic.Text.ToString()) + int.Parse(mtxtHA.Text.ToString()) + int.Parse(mtxtTa.Text.ToString()) + int.Parse(mtxtothers.Text.ToString());
           mtxtTotal.Text = amount.ToString();
        }




        /// <summary>
        /// done to calculate total when values entered in any
        /// of the salary feild changes
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void mtxtbasic_TextChanged(object sender, EventArgs e)
        {
            totalsumaddaction();
          
        }

        private void mtxtTa_TextChanged(object sender, EventArgs e)
        {
            totalsumaddaction();
        }

        private void mtxtHA_TabIndexChanged(object sender, EventArgs e)
        {
            totalsumaddaction();
        }

        private void mtxtothers_TextChanged(object sender, EventArgs e)
        {
            totalsumaddaction();
        }

        private void btnDocumentedit_Click(object sender, EventArgs e)
        {

        }




 







    }
}
